<?php
    // includes

    include 'includes/config.php';
    include 'includes/webservice.php';
    include 'includes/xml.php';
    include 'includes/funcoes.php';
    include 'includes/retorno.php';
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        

    <?php
        
        function set($op,$op2){
            
            if($op == $op2){
                return 'checked=""';
            }
        }
        
        function arquivoXML($bd_ingres,$tpo, $num){
            $arq_xml_rtn = '';
            $arq_xml_sta = '';
            $arq_xml_err = '';
            
            $sql = "SELECT con_cod,tistrn_tpo,tistrn_num_ori,tistrn_num_ori_prr,tistrn_num, tistrn_dat
            FROM tistrn 
            WHERE tistrn_tpo = '".$tpo."'
            AND tistrn_num_ori = " . $num . " ORDER BY tistrn_num DESC"; 
            $result2 = odbc_exec($bd_ingres, $sql);
            while(odbc_fetch_row($result2)) {
                $data = odbc_result($result2, 'tistrn_dat');  
                
                $arq_xml_rtn = 'arquivos/xml_rtn/'.substr($data,0,4).'/'.substr($data,5,2).'/'.substr($data,8,2).'/'.odbc_result($result2, 'tistrn_num').'.xml';
                if(!file_exists($arq_xml_rtn)) $arq_xml_rtn = '';
                
                $arq_xml_sta = 'arquivos/xml_sta/'.substr($data,0,4).'/'.substr($data,5,2).'/'.substr($data,8,2).'/'.odbc_result($result2, 'tistrn_num').'_rtn.xml';
                if(!file_exists($arq_xml_sta)) $arq_xml_sta = '';
                
                break;
            }
            
            if(strlen($arq_xml_rtn) > 0) $arq_xml_rtn = ' <a href="'.$arq_xml_rtn.'" target="_blank">Retorno: '.$arq_xml_rtn.'</a>';
            
            if(strlen($arq_xml_sta) > 0) $arq_xml_sta = ' <a href="'.$arq_xml_sta.'" target="_blank">Verificacao: '.$arq_xml_sta.'</a>';
            
            $arq_xml_err = 'arquivos/xml_err/'.date('Y').'/'.date('m').'/'.date('d').'/'.$num.'.xml';
            if(!file_exists($arq_xml_err)) $arq_xml_err = '';
            if(strlen($arq_xml_err) > 0) $arq_xml_err = ' <a href="'.$arq_xml_err.'" target="_blank">Erro: '.$arq_xml_err.'</a>';
            
            return $arq_xml_rtn.$arq_xml_sta.$arq_xml_err;
            
        }
        
        
        $funcoes = new Funcoes();
        
        $op = @$_GET['op'];
        $data = @$_GET['data'];
        $where = '';
        
        
        ?>
        
        <form action="" method="GET">
            <table border="1">
                <tr>
                    <td><input type="radio" name="op" value="10" <?php echo set(10, $op); ?>> Guias em P</td>
                    <td><input type="radio" name="op" value="11" <?php echo set(11, $op); ?>> Guias em E</td>
                    <td>Data <input type="text" name="data" value="<?php echo $data; ?>"> </td>
                    <td><button type="submit">Pesquisar</button></td>
                </tr>
            </table>
        </form>
        <br>
        
        <?php
        
       
        // ftp p ee
        
        if($op == 9){
            
            //and tisint_dat_emi_gui >= '01062014' 
            //and tisint_dat_emi_gui < '01092014' 
            $sql = "select distinct(tisint_num), tisint_snh, tisint_num_crt, tisint_dat_emi_gui, pac.pac_cod, pac.pac_nom
            from tisint , pac          
            where con_cod IN(4,59,183,190,252) and tisint.pac_cod = pac.pac_cod
            and tisint_dat_emi_gui >= '01092014' 
            and tisint_sta in('P') ORDER BY tisint_dat_emi_gui ASC";
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){     
                $tisint_snh = '';
                $tisint_num = odbc_result($query, 'tisint_num'); 
                $tisint_snh = odbc_result($query, 'tisint_snh'); 
                $tisint_num_crt = odbc_result($query, 'tisint_num_crt');
                $tisint_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tisint_dat_emi_gui')));
                $pac_cod =  odbc_result($query, 'pac_cod'); 
                $pac_nom =  odbc_result($query, 'pac_nom');   

                echo $pac_cod.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_num_crt.' | '.$tissdt_dat_emi_gui;

                /*
                echo $tisint_num_crt.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_dat_emi_gui;
                echo ' | ';
                $query2 = "SELECT tisintpcd_cod_pcd_sol, tisintpcd_des_pcd_sol, tisintpcd_qtd_pcd_sol "
                        . " FROM tisintpcd WHERE tisintpcd_sta<>'C' AND tisint_num=" . $tisint_num ;
                $result = odbc_exec($bd_ingres, $query2);
                while (odbc_fetch_row($result)) {
                    echo 'COD:'.odbc_result($result, 'tisintpcd_cod_pcd_sol').' NOM: '.odbc_result($result, 'tisintpcd_des_pcd_sol').' QDT:'.odbc_result($result, 'tisintpcd_qtd_pcd_sol').', ';
                }
                */

                echo '<br>';  
            }
            echo '<br>';


            //AND p.tisintprr_dat >= '01062014'
            //AND p.tisintprr_dat < '01092014'
            $sql = "SELECT p.tisintprr_dat, t.tisint_num, t.tisint_snh, pa.pac_nom, pa.pac_cod,
                    p.tisintprr_num, p.tisintprr_snh, p.tisintprr_qtd_dia_sol,
                    p.tisintprr_qtd_aut, p.tisintprr_sta, p.tisintprr_tpo_aco_aut, t.tisint_num_crt, t.tisint_dat_emi_gui
                    FROM tisintprr p, tisint t , pac pa    
                    WHERE p.tisint_num = t.tisint_num AND t.tisint_sta = 'A' AND t.pac_cod = pa.pac_cod
                    AND p.tisintprr_dat >= '01092014'
                    AND t.con_cod IN(4,59,183,190,252)
                    AND p.tisintprr_tpo_ori = 1
                    AND p.tisintprr_sta in('P')
                    ";  
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){ 
                $tisint_snh = '';
                $tisint_num = odbc_result($query, 'tisint_num'); 
                $tisint_snh = odbc_result($query, 'tisint_snh'); 
                $tisint_num_crt = odbc_result($query, 'tisint_num_crt');
                $tisint_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tisint_dat_emi_gui')));
                $pac_cod =  odbc_result($query, 'pac_cod'); 
                $pac_nom =  odbc_result($query, 'pac_nom');        

                echo $pac_cod.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_num_crt.' | '.$tissdt_dat_emi_gui;

                //echo $tisint_num_crt.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_dat_emi_gui;
                echo '<br>';  
            }

            echo '<br>';
            
            //and tissdt_dat_emi_gui >= '01062014' 
            //and tissdt_dat_emi_gui < '01092014' 
            
            $sql = "select distinct(tissdt_num), tissdt_snh, tissdt_num_crt, tissdt_dat_emi_gui, tissdt_tpo_gui_ori, tissdt_num_gui_ori, pac.pac_cod
                , pac.pac_nom
            from tissdt  , pac          
            where con_cod IN(4,59,183,190,252) and tissdt.pac_cod = pac.pac_cod    
            and tissdt_dat_emi_gui >= '01092014' 
            AND	tissdt_tpo_env	= 'S'
            and tissdt_sta in('P') ORDER BY pac_cod ASC";
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){     
                $tissdt_snh = '';
                $tissdt_num = odbc_result($query, 'tissdt_num'); 
                $tissdt_snh = odbc_result($query, 'tissdt_snh'); 
                $tissdt_num_crt = odbc_result($query, 'tissdt_num_crt');
                $tissdt_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tissdt_dat_emi_gui')));
                $tissdt_tpo_gui_ori = odbc_result($query, 'tissdt_tpo_gui_ori');  
                $tissdt_num_gui_ori = odbc_result($query, 'tissdt_num_gui_ori');  
                $pac_cod = odbc_result($query, 'pac_cod');
                $pac_nom =  odbc_result($query, 'pac_nom');   

                $sta = '';
                $tpo = '';
                
                if($tissdt_tpo_gui_ori == 'S'){
                    $query2 = 'SELECT tissdt_sta FROM tissdt where tissdt_num = '.$tissdt_num_gui_ori;
                    $result = odbc_exec($bd_ingres, $query2);   
                    while(odbc_fetch_row($result)){   
                        $sta = odbc_result($result, 'tissdt_sta');
                        $tpo = 'SDT';
                    }  
                    if($sta == ''){
                        // correção por defeito da troca convenio
                        $qrup = 'SELECT tisint_sta FROM tisint where tisint_num = '.$tissdt_num_gui_ori;
                        $result = odbc_exec($bd_ingres, $qrup); 
                        while(odbc_fetch_row($result)){   
                            $sta = odbc_result($result, 'tisint_sta');  
                            $tpo = 'TRC P/ I';
                            
                            // correção por defeito da troca convenio
                            $updt = "update tissdt set tissdt_tpo_gui_ori ='I' where tissdt_num = ".$tissdt_num;                           
                            $up_op = odbc_exec($bd_ingres, $updt);
                            $commit = odbc_commit($bd_ingres);
                        } 
                    }
                    
                }else if($tissdt_tpo_gui_ori == 'I'){
                    $query2 = 'SELECT tisint_sta FROM tisint where tisint_num = '.$tissdt_num_gui_ori;
                    $result = odbc_exec($bd_ingres, $query2); 
                    while(odbc_fetch_row($result)){   
                        $sta = odbc_result($result, 'tisint_sta');  
                        $tpo = 'INT';
                    } 
                }

                echo $pac_cod.' | '. $pac_nom .' | '.$tissdt_tpo_gui_ori.' | '.$tissdt_num.' | '.$tissdt_snh.' | '.$tissdt_num_crt.' | '.$tissdt_dat_emi_gui;
                if($sta > '') echo ' | '.$tissdt_tpo_gui_ori.' | '.$tissdt_num_gui_ori.' | '.$tpo.' |-'.$sta;

                /* para unimed
                echo $tissdt_num_crt.' | '. $pac_nom .' | '.$tissdt_num.' | '.$tisint_snh.' | '.$tissdt_dat_emi_gui;
                echo ' | ';
                $query3 = "SELECT tissdtpcd_cod_pcd_sol, tissdtpcd_des_pcd_sol, tissdtpcd_qtd_pcd_sol "
                        . " FROM tissdtpcd WHERE tissdtpcd_sta<>'C' AND tissdt_num=" . $tissdt_num ;
                $result = odbc_exec($bd_ingres, $query3);
                while (odbc_fetch_row($result)) {
                    echo 'COD:'.odbc_result($result, 'tissdtpcd_cod_pcd_sol').' NOM: '.odbc_result($result, 'tissdtpcd_des_pcd_sol').' QDT:'.odbc_result($result, 'tissdtpcd_qtd_pcd_sol').', ';
                }
                */
                echo '<br>';  
            }
        }
        
        
        // wsd p
        
        // wsd e
        
        
        
        
        if($op == 10 Or $op == 11){
            
            $count = 0;
            
            if($op == 10) $status = 'P';
            else if($op == 11) $status = 'E';
            
            echo 'Guias em '.$status.'<br>';
            
            ?>
            <table border="1">
                <tr>
                    <th>Pac</th>
                    <th>Nom</th>
                    <th>Guia</th>
                    <th>senha</th>
                    <th>Carteira</th>
                    <th>Data</th>
                    <th>Tipo</th>
                    <th>Guia Principal</th>
                    <th></th>
                </tr>
            <?php
            
            $where = " and tisint_dat_emi_gui >= '01092014' ";
            if($data > '') $where .= "and tisint_dat_emi_gui = '".$data."'";
            
            $sql = "select distinct(tisint_num), tisint_snh, tisint_num_crt, tisint_dat_emi_gui, pac.pac_cod, pac.pac_nom
            from tisint , pac          
            where con_cod IN(4,59,183,190,252) and tisint.pac_cod = pac.pac_cod
            ".$where." 
            and tisint_sta in('".$status."') ORDER BY tisint_dat_emi_gui ASC";
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){     
                $tisint_snh = '';
                $tisint_num = odbc_result($query, 'tisint_num'); 
                $tisint_snh = odbc_result($query, 'tisint_snh'); 
                $tisint_num_crt = odbc_result($query, 'tisint_num_crt');
                $tisint_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tisint_dat_emi_gui')));
                $pac_cod =  odbc_result($query, 'pac_cod'); 
                $pac_nom =  odbc_result($query, 'pac_nom');   
                $count++;
                //echo $pac_cod.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_num_crt.' | '.$tisint_dat_emi_gui;

                /*
                echo $tisint_num_crt.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_dat_emi_gui;
                echo ' | ';
                $query2 = "SELECT tisintpcd_cod_pcd_sol, tisintpcd_des_pcd_sol, tisintpcd_qtd_pcd_sol "
                        . " FROM tisintpcd WHERE tisintpcd_sta<>'C' AND tisint_num=" . $tisint_num ;
                $result = odbc_exec($bd_ingres, $query2);
                while (odbc_fetch_row($result)) {
                    echo 'COD:'.odbc_result($result, 'tisintpcd_cod_pcd_sol').' NOM: '.odbc_result($result, 'tisintpcd_des_pcd_sol').' QDT:'.odbc_result($result, 'tisintpcd_qtd_pcd_sol').', ';
                }
                */

                //echo '<br>';  
                
                $arq_xml = arquivoXML($bd_ingres,'TISINT', $tisint_num);
                
                ?>
                <tr>
                    <td><?php echo $pac_cod; ?></td>
                    <td><?php echo $pac_nom; ?></td>
                    <td><?php echo $tisint_num; ?></td>
                    <td><?php echo $tisint_snh; ?></td>
                    <td><?php echo $tisint_num_crt; ?></td>
                    <td><?php echo $tisint_dat_emi_gui; ?></td>
                    <td>TISINT</td>
                    <td></td>
                    <td><?php echo $arq_xml;?></td>
                </tr>
                <?php
            }

            $where = " AND p.tisintprr_dat >= '01092014' AND p.tisintprr_dat >= '01092014' ";
            if($data > '') $where .= " and t.tisint_dat_emi_gui = '".$data."' ";
            
            $sql = "SELECT p.tisintprr_dat, t.tisint_num, t.tisint_snh, pa.pac_nom, pa.pac_cod,
                    p.tisintprr_num, p.tisintprr_snh, p.tisintprr_qtd_dia_sol,
                    p.tisintprr_qtd_aut, p.tisintprr_sta, p.tisintprr_tpo_aco_aut, t.tisint_num_crt, t.tisint_dat_emi_gui
                    FROM tisintprr p, tisint t , pac pa    
                    WHERE p.tisint_num = t.tisint_num AND t.tisint_sta = 'A' AND t.pac_cod = pa.pac_cod
                    AND t.con_cod IN(4,59,183,190,252)
                    AND p.tisintprr_tpo_ori = 1
                    AND p.tisintprr_sta in('".$status."') 
                     ".$where."  
                    ";  
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){ 
                $tisint_snh = '';
                $tisint_num = odbc_result($query, 'tisint_num'); 
                $tisint_snh = odbc_result($query, 'tisint_snh'); 
                $tisint_num_crt = odbc_result($query, 'tisint_num_crt');
                $tisint_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tisint_dat_emi_gui')));
                $pac_cod =  odbc_result($query, 'pac_cod'); 
                $pac_nom =  odbc_result($query, 'pac_nom');        
                $count++;
                //echo $pac_cod.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_num_crt.' | '.$tisint_dat_emi_gui;

                //echo $tisint_num_crt.' | '. $pac_nom .' | '.$tisint_num.' | '.$tisint_snh.' | '.$tisint_dat_emi_gui;
                //echo '<br>';  
                
                $arq_xml = arquivoXML($bd_ingres,'TISINTPRR', $tisint_num);
                
                ?>
                <tr>
                    <td><?php echo $pac_cod; ?></td>
                    <td><?php echo $pac_nom; ?></td>
                    <td><?php echo $tisint_num; ?></td>
                    <td><?php echo $tisint_snh; ?></td>
                    <td><?php echo $tisint_num_crt; ?></td>
                    <td><?php echo $tisint_dat_emi_gui; ?></td>
                    <td>TISINTPRR</td>
                    <td></td>
                    <td><?php echo $arq_xml;?></td>
                </tr>
                <?php
            }
            
            $where = " and tissdt_dat_emi_gui >= '01092014' ";
            if($data > '') $where .= "and tissdt_dat_emi_gui = '".$data."'";
            
            $sql = "select distinct(tissdt_num), tissdt_snh, tissdt_num_crt, tissdt_dat_emi_gui, tissdt_tpo_gui_ori, tissdt_num_gui_ori, pac.pac_cod
                , pac.pac_nom
            from tissdt  , pac          
            where con_cod IN(4,59,183,190,252) and tissdt.pac_cod = pac.pac_cod    
             ".$where."  
            and tissdt_sta in('".$status."') ORDER BY pac_cod ASC";
            $query = odbc_exec($bd_ingres, $sql);   
            while(odbc_fetch_row($query)){     
                $tissdt_snh = '';
                $tissdt_num = odbc_result($query, 'tissdt_num'); 
                $tissdt_snh = odbc_result($query, 'tissdt_snh'); 
                $tissdt_num_crt = odbc_result($query, 'tissdt_num_crt');
                $tissdt_dat_emi_gui = date("d/m/Y", strtotime(odbc_result($query, 'tissdt_dat_emi_gui')));
                $tissdt_tpo_gui_ori = odbc_result($query, 'tissdt_tpo_gui_ori');  
                $tissdt_num_gui_ori = odbc_result($query, 'tissdt_num_gui_ori');  
                $pac_cod = odbc_result($query, 'pac_cod');
                $pac_nom =  odbc_result($query, 'pac_nom');   
                $count++;
                
                $sta = '';
                if($tissdt_tpo_gui_ori == 'S'){
                    $query2 = 'SELECT tissdt_sta FROM tissdt where tissdt_num = '.$tissdt_num_gui_ori;
                    $result = odbc_exec($bd_ingres, $query2);   
                    while(odbc_fetch_row($result)){   
                        $sta = odbc_result($result, 'tissdt_sta');  
                    }  
                }else if($tissdt_tpo_gui_ori == 'I'){
                    $query2 = 'SELECT tisint_sta FROM tisint where tisint_num = '.$tissdt_num_gui_ori;
                    $result = odbc_exec($bd_ingres, $query2); 
                    while(odbc_fetch_row($result)){   
                        $sta = odbc_result($result, 'tisint_sta');  
                    } 
                }

                //echo $pac_cod.' | '. $pac_nom .' | '.$tissdt_tpo_gui_ori.' | '.$tissdt_num.' | '.$tissdt_snh.' | '.$tissdt_num_crt.' | '.$tissdt_dat_emi_gui;
                //if($sta > '') echo ' | '.$tissdt_num_gui_ori.' | '.$sta;

                /* para unimed
                echo $tissdt_num_crt.' | '. $pac_nom .' | '.$tissdt_num.' | '.$tisint_snh.' | '.$tissdt_dat_emi_gui;
                echo ' | ';
                $query3 = "SELECT tissdtpcd_cod_pcd_sol, tissdtpcd_des_pcd_sol, tissdtpcd_qtd_pcd_sol "
                        . " FROM tissdtpcd WHERE tissdtpcd_sta<>'C' AND tissdt_num=" . $tissdt_num ;
                $result = odbc_exec($bd_ingres, $query3);
                while (odbc_fetch_row($result)) {
                    echo 'COD:'.odbc_result($result, 'tissdtpcd_cod_pcd_sol').' NOM: '.odbc_result($result, 'tissdtpcd_des_pcd_sol').' QDT:'.odbc_result($result, 'tissdtpcd_qtd_pcd_sol').', ';
                }
                */
                //echo '<br>';  
                $arq_xml = arquivoXML($bd_ingres,'TISSDT', $tissdt_num);
                
                ?>
                <tr>
                    <td><?php echo $pac_cod; ?></td>
                    <td><?php echo $pac_nom; ?></td>
                    <td><?php echo $tissdt_num; ?></td>
                    <td><?php echo $tissdt_snh; ?></td>
                    <td><?php echo $tissdt_num_crt; ?></td>
                    <td><?php echo $tissdt_dat_emi_gui; ?></td>
                    <td>TISSDT</td>
                    <td><?php echo $tissdt_num_gui_ori.' - '.$sta; ?></td>
                    <td><?php echo $arq_xml;?></td>
                </tr>
                <?php
            }
            
            
            ?>
            </table>
            <?php    
            
            echo 'TOTAL DE GUIAS: '.$count;
        }
        
        
        
        odbc_close($bd_ingres);
    ?>
    </body>
</html>

